package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.repository.DestinationMongoRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    
    @Autowired
    private DestinationMongoRepository destinationMongoRepository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private RegionServiceImpl regionService;
    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationMongoRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationMongoRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationMongoRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationMongoRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationMongoRepository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        // 通过refIds获取到region 对象
        Region region = regionService.get(rid);
        // 在获取region 对象中的所有refIds
        List<String> refIds = region.getRefIds();
        // 通过refIds 从数据库获取到所有 Destination 对象
        List<Destination> list = destinationMongoRepository.getByIdIn(refIds);
        return list;
    }

    @Override
    public void saveOrUpdate(Destination destination) {
        if(StringUtils.hasLength(destination.getId())){
            this.update(destination);
        }else {
            this.save(destination);
        }
    }

    @Override
    public Page<Destination> query(QueryObject qo) {
        // 创建查询对象
        Query query = new Query();
        // 添加高级分页查询条件(mongoDB 语法)  Criteria:条件
        if(StringUtils.hasLength(qo.getKeyword())){
            // 在查询对象里添加条件,where name  regex (填入数据)
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        if(StringUtils.hasLength(qo.getParentId())){
            // 在查询对象里添加条件,如果id 为空就是根目录
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }
        // 查询总数据数
        Long count = template.count(query, Destination.class);
        // 设置查询条件对象
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                                                                Sort.Direction.ASC, "_id");
        // 把分页条件设置进查询对象
        query.with(pageable);
        List<Destination> list = template.find(query, Destination.class);
        return new PageImpl<>(list ,pageable, count);

    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        //创建一个空集合用来装parentId 对应的值
        List list = new ArrayList<>();
        // 如果当前parentId 是否有长度,说明是根目录
        if(!StringUtils.hasLength(parentId)){
            return list;
        }
        // 判断当前parentId 是否有长度
        while (StringUtils.hasLength(parentId)){
            // 如果有长度就获取当前id的值
            Destination dest = this.get(parentId);
            //把值添加到list
            list.add(dest);
            //把他爸的id 赋值给当前的parentId 再循环,知道id为空
            parentId = dest.getParentId();
        }
        // 把获取到的list 元素前后顺序翻转
        Collections.reverse(list);
        return list;
    }

    @Override
    public Object queryDestByRegionId(String regionId) {
        List<Destination> list = new ArrayList<>();
        if("-1".equals(regionId)){
            list = destinationMongoRepository.findByParentName("中国");
        }else {
            list = this.getDestByRegionId(regionId);
        }

        Pageable pageable = PageRequest.of(0,5 ,Sort.Direction.ASC , "_id");

        for (Destination dest: list) {
            List<Destination> children = destinationMongoRepository.findByParentId(dest.getId() ,pageable);
            dest.setChildren(children);
        }
        return list;
    }
}
